package com.jeff.android.lab;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * @author: duff
 * @date: 2019/3/15
 * @since: 1.0.0
 */
public abstract class BaseRecyclerViewFragment extends BaseFragment {
    @BindView(R.id.ptr) PtrClassicFrameLayout mPtr;
    @BindView(R.id.rlv) RecyclerView mRecyclerView;

    @Override
    protected int getRootLayout() {
        return R.layout.common_ptr_recycler_view;
    }

    @Override
    protected void initView() {
        super.initView();
        mPtr.setEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mAdapter.setNewData(getData());
    }

    protected abstract List<String> getData();

    protected abstract void launch(String item);

    BaseQuickAdapter mAdapter =
            new BaseQuickAdapter<String, com.chad.library.adapter.base.BaseViewHolder>(R.layout.common_simple_item_text) {
                @Override
                protected void convert(BaseViewHolder helper, final String item) {
                    TextView text = helper.getView(R.id.tv_simple_item);
                    text.setText(item);

                    helper.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            launch(item);
                        }
                    });
                }
            };

}
